import Vue from 'vue'
import Router from 'vue-router'

// 将VueRouter 挂载为Vue的插件
Vue.use(Router)

/* Layout 布局容器 包含了项目最基础的结构 */
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
import approvalsRouter from './modules/approvals'
import departmentsRouter from './modules/departments'
import employeesRouter from './modules/employees'
import permissionRouter from './modules/permission'
import attendancesRouter from './modules/attendances'
import salarysRouter from './modules/salarys'
import settingRouter from './modules/setting'
import socialRouter from './modules/social'
export const asyncRoutes = [
  departmentsRouter, // 组织结构
  approvalsRouter,

  employeesRouter,
  permissionRouter,
  attendancesRouter,
  salarysRouter,
  settingRouter,
  socialRouter

]
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true // 不生成左侧菜单
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/', // 一般是代表默认路由
    component: Layout, // 展示的Loayout 布局容器组件
    redirect: '/dashboard', // 重定向
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/dashboard/index'),
        meta: { title: '首页', icon: 'dashboard' }
      }
    ]
  },
  {
    path: '/excel',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '',
        name: 'excel',
        component: () => import('@/views/excel')
      }
    ]
  }

  // 404 page must be placed at the end !!!
  // { path: '*', redirect: '/404', hidden: true }
  // 问题1:做了动态路由和addRoutes以后  为何在动态路由路径时,'刷新'网页会直接进入404
  // 1. 刷新是 所有代码重新执行 ->路由new Router 和vuex的所有值回归初始化
  // 2. 地址栏的地址还是动态路由路径 上来刚开始就和new Router 里规则(当前静态数组里挨个对象path配),匹配失败来到了当前path:* 被重定向到404
  // 问题2:那我们不是发请求拿到用户信息数据 过滤出动态的 添加上去那应该能匹配到呀
  // 1.网络请求时异步的 在你的数据回来之前 我们的路由已经尝试匹配了一次 找不到直接404le
  // 先匹配路由规则->如果规则失败直接走redirect重定向->路由全局前置守卫->决定是否放行->路由守卫里有个异步的网络请求,回来加入动态的路由规则进路由表
  // 解决:把path兜底的404路由规则放到动态路由数组houmian
  // 又出现了问题
  // 刷新白屏了
  // 原因同上 只不过现在也没有404了 所以路由出口处什么组件都不挂
  // 解决: 在加载上动态路由路径后面加一次路由跳转
]

// 对VueRouter进行实例化
const createRouter = () =>
  new Router({
    // mode: 'history', // require service support
    scrollBehavior: () => ({ y: 0 }),
    // routes: [...constantRoutes, ...asyncRoutes]// 配置路由规则
    routes: [...constantRoutes]// 让它只显示静态路由
  })

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
// 清除内存里router路由对象能匹配的路由
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // matcher 所谓的内存里能匹配到的路由规则数组->原来动态添加的给覆盖掉
}

export default router
